﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WisDom.SmartTMS.Dto;
using WisDom.SmartTMS.Entity;
using WisDom.SmartTMS.InterFace;
using Microsoft.EntityFrameworkCore;
using Abp.Application.Services.Dto;
using System.Linq.Dynamic.Core;

namespace WisDom.SmartTMS.Services
{
    /// <summary>
    /// 离职办理AppService实现类
    /// </summary>
    public class LeaveDealAppService : ApplicationService, ILeaveDealAppService
    {
        /// <summary>
        /// 注入上下文
        /// </summary>
        private readonly IRepository<LeaveDeal, Guid> _leaveDeals;
        public LeaveDealAppService(IRepository<LeaveDeal, Guid> leaveDeals)
        {
            _leaveDeals = leaveDeals;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="leaveDealDtoCreate"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<LeaveDealDtoCreate> Add([FromBody] LeaveDealDtoCreate leaveDealDtoCreate)
        {
            LeaveDeal m = new(
               GuidGenerator.Create(),
               leaveDealDtoCreate.LDName,
               leaveDealDtoCreate.DpID,
               leaveDealDtoCreate.Position,
               leaveDealDtoCreate.Principal,
               leaveDealDtoCreate.EntryTimes,
               leaveDealDtoCreate.LastDay,
               leaveDealDtoCreate.Reasons,
               leaveDealDtoCreate.ApprovalStatus,
               leaveDealDtoCreate.CreateTimes
               );
            var result = await _leaveDeals.InsertAsync(m);
            return ObjectMapper.Map<LeaveDeal, LeaveDealDtoCreate>(result);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task Delete(Guid id)
        {
            await _leaveDeals.DeleteAsync(id);
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<LeaveDealDto> Edit(Guid id)
        {
            var data = await _leaveDeals.GetAsync(id);
            return ObjectMapper.Map<LeaveDeal, LeaveDealDto>(data);
        }

        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<PagedResultDto<LeaveDealDto>> PaymentShow(PagedAndSortedResultRequestDto input)
        {
            //创建一个查询对象
            var query = _leaveDeals
                .OrderBy(input.Sorting);
            //总条数
            var totalCount = await query.CountAsync();
            //从仓储获取实体数据对象
            List<LeaveDeal> leaveDeal = await query
                .Skip(input.SkipCount)
                .Take(input.MaxResultCount).ToListAsync();
            //进行实体转换Dto
            List<LeaveDealDto> leaveDealDto =
                ObjectMapper.Map<List<LeaveDeal>, List<LeaveDealDto>>(leaveDeal);
            //返回结果
            return new PagedResultDto<LeaveDealDto>(totalCount, leaveDealDto);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="material"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<LeaveDealDto> Upt([FromForm] LeaveDealDto dealDto)
        {
            LeaveDeal m = new(

               dealDto.Id,
                dealDto.LDName,
                dealDto.DpID,
                dealDto.Position,
                dealDto.Principal,
                dealDto.EntryTimes,
                dealDto.LastDay,
                dealDto.Reasons,
                dealDto.ApprovalStatus,
                dealDto.CreateTimes
                );
            var result = await _leaveDeals.UpdateAsync(m);
            return ObjectMapper.Map<LeaveDeal, LeaveDealDto>(result);
        }

        /// <summary>
        /// 事务
        /// </summary>
        /// <param name="leaveDealDtoCreate"></param>
        /// <param name="creatorID"></param>
        /// <param name="LcreatorID"></param>
        /// <returns></returns>
        //public int AddLeaveDeal(LeaveDealDtoCreate leaveDealDtoCreate, List<int> creatorID, int LcreatorID)
        //{
        //    _leaveDeals.Open();
        //}
    }
}
